﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace QLSVPT1h
{
    public partial class ChinhSuaHS : Form
    {
        public ChinhSuaHS()
        {
            InitializeComponent();
        }

        private void ChinhSuaHS_Load(object sender, EventArgs e)
        {
            string connectionString = "Data Source=PC-200908251039;Initial Catalog=QLHS;Integrated Security=True";
            SqlConnection conn = new SqlConnection(connectionString);
            conn.Open();
            string commandText = "select MaHS from HOCSINH";
            SqlDataAdapter dataAdapter = new SqlDataAdapter(commandText, connectionString);
            DataSet dataSet = new DataSet();
            dataAdapter.Fill(dataSet, "HOCSINH");
            DataTable dataTable = dataSet.Tables[0];

            foreach (DataRow dataRow in dataTable.Rows)
            {
                listBox1.Items.Add(dataRow["MaHS"]);
            }

            comboBox2.Text = "TPHCM";
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string connectionString = "Data Source=PC-200908251039;Initial Catalog=QLHS;Integrated Security=True";
            SqlConnection conn = new SqlConnection(connectionString);

            conn.Open();

            string maHS = listBox1.SelectedItem.ToString();
            string commandText = "select maHS from HOCSINH where MaHS = @maHS";
            SqlCommand comm = new SqlCommand(commandText, conn);
            comm.Parameters.Add("@maHS", SqlDbType.VarChar, 7);
            comm.Parameters["@maHS"].Value = maHS;
            object o = comm.ExecuteScalar();
            textBox1.Text = o.ToString();

            commandText = "select HoTen from HOCSINH where MaHS = @maHS";
            comm = new SqlCommand(commandText, conn);
            comm.Parameters.Add("@maHS", SqlDbType.VarChar, 7);
            comm.Parameters["@maHS"].Value = maHS;
            o = comm.ExecuteScalar();
            textBox3.Text = o.ToString();

            commandText = "select NgaySinh from HOCSINH where MaHS = @maHS";
            comm = new SqlCommand(commandText, conn);
            comm.Parameters.Add("@maHS", SqlDbType.VarChar, 7);
            comm.Parameters["@maHS"].Value = maHS;
            o = comm.ExecuteScalar();
            textBox4.Text = o.ToString();

            commandText = "select DiaChi from HOCSINH where MaHS = @maHS";
            comm = new SqlCommand(commandText, conn);
            comm.Parameters.Add("@maHS", SqlDbType.VarChar, 7);
            comm.Parameters["@maHS"].Value = maHS;
            o = comm.ExecuteScalar();
            textBox5.Text = o.ToString();

            commandText = "select DienThoai from HOCSINH where MaHS = @maHS";
            comm = new SqlCommand(commandText, conn);
            comm.Parameters.Add("@maHS", SqlDbType.VarChar, 7);
            comm.Parameters["@maHS"].Value = maHS;
            o = comm.ExecuteScalar();
            textBox6.Text = o.ToString();

            commandText = "select MaLop from HOCSINH where MaHS = @maHS";
            comm = new SqlCommand(commandText, conn);
            comm.Parameters.Add("@maHS", SqlDbType.VarChar, 7);
            comm.Parameters["@maHS"].Value = maHS;
            o = comm.ExecuteScalar();
            comboBox1.Text = o.ToString();

            commandText = "select Phai from HOCSINH where MaHS = @maHS";
            comm = new SqlCommand(commandText, conn);
            comm.Parameters.Add("@maHS", SqlDbType.VarChar, 7);
            comm.Parameters["@maHS"].Value = maHS;
            o = comm.ExecuteScalar();
            string phai = o.ToString();
            if (phai == "Nam")
            {
                radioButton1.Checked = true;
                radioButton2.Checked = false;
            }
            else
            {
                radioButton1.Checked = false;
                radioButton2.Checked = true;
            }

            conn.Close();

        }

        private void button2_Click(object sender, EventArgs e)
        {
            string connectionString = "Data Source=PC-200908251039;Initial Catalog=QLHS;Integrated Security=True";
            SqlConnection conn = new SqlConnection(connectionString);
            conn.Open();
            string maHS_new = textBox1.Text;
            string maHS_old = listBox1.SelectedItem.ToString();
            string commandText = "update HOCSINH set maHS = @maHS_new where maHS = @maHS_old";
            SqlCommand comm = new SqlCommand(commandText, conn);

            comm.Parameters.Add("@maHS_new", SqlDbType.VarChar, 7);
            comm.Parameters["@maHS_new"].Value = maHS_new;
            comm.Parameters.Add("@maHS_old", SqlDbType.VarChar, 7);
            comm.Parameters["@maHS_old"].Value = maHS_old;
            comm.ExecuteNonQuery();

            commandText = "update HOCSINH set maLop = @maLop where maHS = @maHS_old";
            string maLop = comboBox1.SelectedItem.ToString();
            comm = new SqlCommand(commandText, conn);
            comm.Parameters.Add("@maLop", SqlDbType.VarChar, 5);
            comm.Parameters["@maLop"].Value = maLop;
            comm.Parameters.Add("@maHS_old", SqlDbType.VarChar, 7);
            comm.Parameters["@maHS_old"].Value = maHS_old;
            comm.ExecuteNonQuery();

            commandText = "update HOCSINH set HoTen = @HoTen where maHS = @maHS_old";
            string hoTen = textBox3.Text;
            comm = new SqlCommand(commandText, conn);
            comm.Parameters.Add("@hoTen", SqlDbType.NVarChar, 50);
            comm.Parameters["@hoTen"].Value = hoTen;
            comm.Parameters.Add("@maHS_old", SqlDbType.VarChar, 7);
            comm.Parameters["@maHS_old"].Value = maHS_old;
            comm.ExecuteNonQuery();

            commandText = "update HOCSINH set NgaySinh = @ngaySinh where maHS = @maHS_old";
            string ngaySinh = textBox4.Text;
            comm = new SqlCommand(commandText, conn);
            comm.Parameters.Add("@ngaySinh", SqlDbType.DateTime);
            comm.Parameters["@ngaySinh"].Value = ngaySinh;
            comm.Parameters.Add("@maHS_old", SqlDbType.VarChar, 7);
            comm.Parameters["@maHS_old"].Value = maHS_old;
            comm.ExecuteNonQuery();

            commandText = "update HOCSINH set Phai = @phai where maHS = @maHS_old";
            string phai;
            if (radioButton1.Checked == true)
                phai = "Nam";
            else
                phai = "Nữ";
            comm = new SqlCommand(commandText, conn);
            comm.Parameters.Add("@phai", SqlDbType.NVarChar, 4);
            comm.Parameters["@phai"].Value = phai;
            comm.Parameters.Add("@maHS_old", SqlDbType.VarChar, 7);
            comm.Parameters["@maHS_old"].Value = maHS_old;
            comm.ExecuteNonQuery();

            commandText = "update HOCSINH set DiaChi = @diaChi where maHS = @maHS_old";
            string diaChi = textBox5.Text;
            comm = new SqlCommand(commandText, conn);
            comm.Parameters.Add("@diaChi", SqlDbType.NVarChar, 50);
            comm.Parameters["@diaChi"].Value = diaChi;
            comm.Parameters.Add("@maHS_old", SqlDbType.VarChar, 7);
            comm.Parameters["@maHS_old"].Value = maHS_old;
            comm.ExecuteNonQuery();

            commandText = "update HOCSINH set DienThoai = @dienThoai where maHS = @maHS_old";
            string dienThoai = textBox6.Text;
            comm = new SqlCommand(commandText, conn);
            comm.Parameters.Add("@dienThoai", SqlDbType.VarChar, 15);
            comm.Parameters["@dienThoai"].Value = dienThoai;
            comm.Parameters.Add("@maHS_old", SqlDbType.VarChar, 7);
            comm.Parameters["@maHS_old"].Value = maHS_old;
            comm.ExecuteNonQuery();
            MessageBox.Show("Dữ liệu đã được cập nhật");
            conn.Close();
        }

        private void ChinhSuaHS_FormClosed(object sender, FormClosedEventArgs e)
        {
            Homepage_Form hp = new Homepage_Form();
            this.Visible = false;
            hp.Show();
        }

        private void label6_Click(object sender, EventArgs e)
        {
            TimKiemHS tk = new TimKiemHS();
            this.Visible = false;
            tk.Show();
        }

        private void label1_Click(object sender, EventArgs e)
        {
            Form1 f1 = new Form1();
            this.Visible = false;
            f1.Show();
        }

        private void label2_Click(object sender, EventArgs e)
        {
            ChinhSuaHS cs = new ChinhSuaHS();
            this.Visible = false;
            cs.Show();
        }

        private void label4_Click(object sender, EventArgs e)
        {
            ThayDoiQD csqd = new ThayDoiQD();
            this.Visible = false;
            csqd.Show();
        }

        private void label3_Click(object sender, EventArgs e)
        {
            Bang_diem_mon bd = new Bang_diem_mon();
            this.Visible = false;
            bd.Show();
        }

        private void label5_Click(object sender, EventArgs e)
        {
            BaoCao bc = new BaoCao();
            this.Visible = false;
            bc.Show();
        }

        private void label10_Click(object sender, EventArgs e)
        {
            Homepage_Form hp = new Homepage_Form();
            this.Visible = false;
            hp.Show();
        }

       

        
    }
}
